package com.qf.shiro;

import com.qf.util.RedisUtil;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;


@Component
public class AppSessionDAO extends AbstractSessionDAO {

    @Autowired
    private RedisUtil redisUtil;

    private final String SESSION_PREFIX = "shiro-session:";

    private final int SESSION_TIME_OUT = 600;

    // 添加
    @Override
    protected Serializable doCreate(Session session) {
        //0. 判断非空
        if(session == null){
            return null;
        }
        //1. 生成sessionId.
        Serializable sessionId = super.generateSessionId(session);
        //2. 将sessionId和session绑定到一起.
        super.assignSessionId(session,sessionId);
        //3. 保存到redis!!!!
        setAndExpireSession(session, sessionId);
        //7. 返回sessionId
        return sessionId;
    }

    private void setAndExpireSession(Session session, Serializable sessionId) {
        byte[] key = SerializationUtils.serialize(SESSION_PREFIX + sessionId);
        //4. 准备一个value.
        byte[] value = SerializationUtils.serialize(session);
        //5. 保存到redis.  设置生存时间.
        redisUtil.setAndExpire(key, value, SESSION_TIME_OUT);
    }


    // 读取
    @Override
    protected Session doReadSession(Serializable sessionId) {
        //1. 判断非空!!
        if(sessionId == null){
            return null;
        }
        System.out.println("去redis查询用户信息!!");
        //2. 封装key,
        byte[] key = SerializationUtils.serialize(SESSION_PREFIX + sessionId);
        //3. 通过key获取value, 重新设置key的生存时间.
        byte[] value = redisUtil.getAndExpire(key,SESSION_TIME_OUT);
        //5. 反序列化.
        Session session = (Session) SerializationUtils.deserialize(value);
        //6. 返回
        return session;
    }


    // 修改
    @Override
    public void update(Session session) throws UnknownSessionException {
        //1. 非空判断.
        if(session == null){
            return;
        }
        //2. 通过session获取sessionId.
        Serializable sessionId = session.getId();
        //3. 设置到redis中. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        setAndExpireSession(session, sessionId);
    }


    // 删除
    @Override
    public void delete(Session session) {
        //1. 非空判断.
        if(session == null){
            return ;
        }
        //2. 获取sessionId.
        Serializable sessionId = session.getId();

        //3. 生成key
        byte[] key = SerializationUtils.serialize(SESSION_PREFIX + sessionId);

        //3. 执行删除
        redisUtil.del(key);
    }


    // 查看全部存活的session
    @Override
    public Collection<Session> getActiveSessions() {
        //1. 生成key
        byte[] pattern = SerializationUtils.serialize(SESSION_PREFIX + "*");
        //2. 通过keys获取全部的Set<byte[]>
        Set<byte[]> keys = redisUtil.keys(pattern);

        //3. 遍历并将byte[]反序列化成session并封装为Set<Session>
        Set<Session> sessionSet = new HashSet<>();
        for (byte[] key : keys) {
            byte[] value = redisUtil.get(key);
            Session session = (Session) SerializationUtils.deserialize(value);
            sessionSet.add(session);
        }

        //4. 返回
        return sessionSet;
    }
}
